package com.greenant.store.activity;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.greenant.store.R;
import com.greenant.store.bean.WithdrawRecord;
import com.greenant.store.service.RetrieveDataAsyn;
import com.greenant.store.service.RetrieveDataListener;
import com.lsarah.utils.SystemBarTintManager;

public class WithdrawListActivity extends BaseActivity {
	private View mViewLayoutPrompt;
	private View mViewLayoutListLayout;
	private TextView mTvTixianMoney;
	private TextView mTvPrompt;
	private ProgressBar mPbPrompt;
	private ListView mListView;
	private WithdrawListAdapter mWithdrawListAdapter;
	private List<WithdrawRecord> mDataList = new ArrayList<WithdrawRecord>();
	
	private DecimalFormat mDecimalFormat;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.withdraw_list_activity,
				getResources().getColor(R.color.firebrick));
		
		mViewLayoutPrompt = findViewById(R.id.withdraw_list_prompt_layout);
		mViewLayoutListLayout = findViewById(R.id.withdraw_list_listview_layout);
		mTvTixianMoney = (TextView) findViewById(R.id.withdraw_list_tixian_money);
		mTvPrompt = (TextView) findViewById(R.id.withdraw_list_prompt_tv);
		mPbPrompt = (ProgressBar) findViewById(R.id.withdraw_list_prompt_pb);
		mListView = (ListView) findViewById(R.id.withdraw_list_listview);
		
		mDecimalFormat = new DecimalFormat("#########.##");
		double yitixianMoney = getIntent().getDoubleExtra("yitixian_money", 0);
		mTvTixianMoney.setText("￥" + mDecimalFormat.format(yitixianMoney));
		
		this.loadData();
	}
	
	private void loadData() {
		mViewLayoutListLayout.setVisibility(View.GONE);
		mViewLayoutPrompt.setVisibility(View.VISIBLE);
		mTvPrompt.setVisibility(View.GONE);
		mPbPrompt.setVisibility(View.VISIBLE);
		RetrieveDataAsyn.getInstance().getWithdrawRecordList(-1, new RetrieveDataListener() {
			@Override
			public void dataRetrieved(int requestId, Object data) {
				List<WithdrawRecord> dataList = (List<WithdrawRecord>)data;
				if(dataList.size() == 0) {
					mViewLayoutListLayout.setVisibility(View.GONE);
					mViewLayoutPrompt.setVisibility(View.VISIBLE);
					mTvPrompt.setVisibility(View.VISIBLE);
					mPbPrompt.setVisibility(View.GONE);
				} else {
					mViewLayoutListLayout.setVisibility(View.VISIBLE);
					mViewLayoutPrompt.setVisibility(View.GONE);
					
					mDataList.addAll(dataList);
					mWithdrawListAdapter = new WithdrawListAdapter(mDataList);
					mListView.setAdapter(mWithdrawListAdapter);
				}
			}
		});
	}

	public void goBack(View view) {
		finish();
	}
	
	private class WithdrawListAdapter extends BaseAdapter {
		private List<WithdrawRecord> dataList;
		private SimpleDateFormat mSimpleDateFormat;

		public WithdrawListAdapter(List<WithdrawRecord> dataList) {
			this.dataList = dataList;
			mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		}
		
		@Override
		public int getCount() {
			return dataList.size();
		}

		@Override
		public Object getItem(int position) {
			if(position >= 0 && position < getCount()) {
				return dataList.get(position);
			}
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View childView = convertView;
			ViewHolder holder = null;
			if(childView == null) {
				childView = LayoutInflater.from(WithdrawListActivity.this).inflate(R.layout.withdraw_list_item, null);
				holder = new ViewHolder(childView);
				childView.setTag(holder);
			} else {
				holder = (ViewHolder)childView.getTag();
			}
			
			WithdrawRecord wr = dataList.get(position);
			// time
			Date date = wr.getCreatedAt();
			String timeStr = "";
			if(date != null) {
				timeStr = mSimpleDateFormat.format(date);
			} 
			holder.getTvTime().setText(timeStr);
			// money
			holder.getTvMoney().setText(mDecimalFormat.format(wr.getWithdraw()));
			// status
			int status = wr.getStatus();
			if(status == 0) {
				holder.getTvStatus().setText(R.string.withdrawals_list_tixian_status_daichuli);
			} else if (status == 1) {
				holder.getTvStatus().setText(R.string.withdrawals_list_tixian_status_chulizhong);
			} else if (status == 2) {
				holder.getTvStatus().setText(R.string.withdrawals_list_tixian_status_yiwancheng);
			}
			
			return childView;
		}
	}
	
	private class ViewHolder {
		private View contentView;
		private TextView tvTime;
		private TextView tvMoney;
		private TextView tvStatus;
		
		public ViewHolder(View contentView) {
			this.contentView = contentView;
		}
		
		public TextView getTvTime() {
			if(tvTime == null) {
				tvTime = (TextView) contentView.findViewById(R.id.withdraw_list_item_time);
			}
			return tvTime;
		}
		
		public TextView getTvMoney() {
			if(tvMoney == null) {
				tvMoney = (TextView) contentView.findViewById(R.id.withdraw_list_item_money);
			}
			return tvMoney;
		}
		
		public TextView getTvStatus() {
			if(tvStatus == null) {
				tvStatus = (TextView) contentView.findViewById(R.id.withdraw_list_item_status);
			}
			return tvStatus;
		}
		
	}
}








